/*
 * This is the MIT license, see also http://www.opensource.org/licenses/mit-license.html
 *
 * Copyright (c) 2001 Brian Pitcher
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package weblech.spider;

import java.io.*;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Random;

import org.apache.log4j.Logger;


public class URLObject {
    private final static Logger log = Logger.getLogger(URLObject.class);

    /** 请求URL   */
    private final URL sourceURL;
    
    /** 内容类型   */
    private final String contentType;
    /**文件里面的内容,如HTML文本   */
    private final byte[] content;

    private final SpiderConfig config;
    
    private int contentSize;

    public URLObject(URL sourceURL, String contentType, byte[] content, SpiderConfig config, int contentSize) {
        this.sourceURL = sourceURL;
        this.contentType = contentType;
        this.content = content;
        this.config = config;
        this.contentSize = contentSize;
    }

    public URLObject(URL sourceURL, SpiderConfig config) {
        this.sourceURL = sourceURL;
        this.config = config;

        String url = sourceURL.toExternalForm().toLowerCase();
        if(url.indexOf(".jpg") != -1) {
            contentType = "image/jpeg";
        }else if (url.indexOf(".gif") != -1) {
            contentType = "image/gif";
        }else {
            contentType = "text/html";
        }
        
        content = new byte[0];
        contentSize = 500;
    }

    public String getContentType() {
        return contentType;
    }

    public boolean isHTML() {
        return contentType.toLowerCase().startsWith("text/html");
    }

    public boolean isXML() {
        return contentType.toLowerCase().startsWith("text/xml");
    }

    public boolean isImage() {
        return contentType.startsWith("image/");
    }
    
    public boolean isMp3() {
    	return contentType.toLowerCase().startsWith("audio/x-mpeg");
    }

    public String getStringContent() {
        return new String(content);
    }

    /** 根据当前URL,得到保存路径*/
    public String convertToFileName() {
        String url = sourceURL.toExternalForm();
        int httpIdx = url.indexOf("http://");
        if(httpIdx == 0) {
            url = url.substring(7);
        }
        if(url.indexOf("/") < 0) {
            url = url + "/";
        }
        if(url.endsWith("/"))  {
            url = url + "index.html";
        }
        try {
        	url = textReplace("?", URLEncoder.encode("?", "application/x-www-form-urlencoded"), url);
        	url = textReplace("&", URLEncoder.encode("&", "application/x-www-form-urlencoded "), url);
        }catch (UnsupportedEncodingException e) {
        	log.debug("URLObject encode exception......");
        }
        url = url.replace("/", "\\");
        String filePath = config.getSaveRootDirectory().getPath() + "\\" + url;
        log.info(filePath);
        return filePath;
    }

    /** 判断以当前URL命名的文件在硬盘是否存在 */
    public boolean existsOnDisk(String saveDir) {
        File f = new File(saveDir);
        return (f.exists() && !f.isDirectory());
    }

    public void writeToFile() {
        writeToFile(getSaveFileName());
    }
    
    private String getSaveFileName() {
        String url = sourceURL.toExternalForm();
        url = rightImageUrl(url);
        String suffix = url.substring(url.lastIndexOf("/") + 1);
        String extension = getExtension(suffix);
        String fileName = getFileName(suffix);
        
        String saveDir = config.getSaveRootDirectory().getPath();
        
        Random ran = new Random();
        String saveFileName = "";
        do {
            saveFileName = saveDir + "\\" + fileName + String.valueOf(ran.nextInt(2000)) + extension;
        } while (existsOnDisk(saveFileName));
        
        return saveFileName;
    }

    public void writeToFile(String fileName) {
        log.debug("writeToFile(" + fileName + ")");
        try {
            File f = new File(fileName);
            f.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(fileName);
            out.write(content);
            out.flush();
            out.close();
            log.info("Write file to " + fileName);
        }catch(IOException ioe) {
            log.warn("IO Exception writing to " + fileName, ioe);
        }
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("URLObject: ");
        sb.append(contentType);
        
        return sb.toString();
    }

    /**
     * 将URL中的?和&替换成编码后的格式
     */
    private String textReplace(String find, String replace, String input) {
        int startPos = 0;
        while(true) {
            int textPos = input.indexOf(find, startPos);
            if(textPos < 0) {
                break;
            }
            input = input.substring(0, textPos) + replace + input.substring(textPos + find.length());
            startPos = textPos + replace.length();
        }
        
        return input;
    }
    
    private String rightImageUrl(String url) {
        if (url.indexOf('?') != -1) {
            url = url.substring(0, url.indexOf('?'));
        }
        
        return url;
    }
    
    private String getExtension(String fileName) {
        String extension = fileName.substring(fileName.lastIndexOf('.'));
        return extension;
    }
    
    private String getFileName(String fileName) {
        String name = fileName.substring(0, fileName.indexOf('.'));
        return name;
    }

    public int getContentSize() {
        return contentSize;
    }
}
